package com.ibangnuo.framework.newLandpay.impl.request;

import com.alibaba.fastjson.JSONObject;
import com.ibangnuo.framework.newLandpay.api.NewLandpayRequest;
import com.ibangnuo.framework.newLandpay.impl.common.ResponseClient;
import com.ibangnuo.framework.newLandpay.impl.common.api.ResponseClientApi;
import com.ibangnuo.framework.newLandpay.impl.response.NewLandMercSearch;
import com.ibangnuo.framework.newLandpay.impl.response.NewLandMercSearchResponse;
import com.ibangnuo.framework.newLandpay.util.HttpParamsUtils;
import com.ibangnuo.framework.newLandpay.util.MD5;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: wwy
 * @Date: 2020-01-11 14:40
 */
@Data
@ToString
@NoArgsConstructor
public class NewLandMercSearchRequest implements NewLandpayRequest {
    private static Logger LOGGER = LoggerFactory.getLogger(NewLandMercSearchRequest.class);
    private Map<String,String> bizContent;//不进行加密的参数
    private Map<String,String> pre;//进行加密的参数
    private final String URL = "http://sandbox.starpos.com.cn/emercapp";
    @Override
    public NewLandMercSearchResponse execute(String key) {
        pre.put("serviceId", "6060300");//交易码
        String preStr = HttpParamsUtils.buildPayValues(pre, false, true);
        String sign = MD5.sign(preStr, key, "UTF-8");
        pre.put("signValue",sign);
        bizContent.putAll(pre);
        ResponseClientApi responseClientApi = new ResponseClient(URL);
        String bizContentJson = JSONObject.toJSONString(bizContent);
        JSONObject jsonObject = JSONObject.parseObject(bizContentJson);
        String response = responseClientApi.request(jsonObject);
        LOGGER.info("新大陆接口响应结果{}",response);
        Map<String,Object> map = JSONObject.parseObject(response, Map.class);
        if(map !=null){
            NewLandMercSearchResponse newLandMercSearchResponse = new NewLandMercSearchResponse();
            newLandMercSearchResponse.setLogNo(String.valueOf(map.get("log_no")));
            newLandMercSearchResponse.setMercId(String.valueOf(map.get("mercId")));
            newLandMercSearchResponse.setMsgCd(String.valueOf(map.get("msg_cd")));
            newLandMercSearchResponse.setMsgDat(String.valueOf(map.get("msg_dat")));
            newLandMercSearchResponse.setSignValue(String.valueOf(map.get("signValue")));
            newLandMercSearchResponse.setMercRemark(String.valueOf(map.get("merc_remark")));
            newLandMercSearchResponse.setBody(JSONObject.toJSONString(map));
            List<Map> list = (List) map.get("REC");
            List<NewLandMercSearch> recList = new ArrayList<>();
            for (Map map1 : list) {
                NewLandMercSearch NewLandMercSearch = new NewLandMercSearch();
                NewLandMercSearch.setTrmNo(String.valueOf(map1.get("trm_no")));
                NewLandMercSearch.setCorgMercNo(String.valueOf(map1.get("corg_merc_no")));
                NewLandMercSearch.setStoeId(String.valueOf(map1.get("stoe_id")));
                recList.add(NewLandMercSearch);
            }
            newLandMercSearchResponse.setRec(recList);
            return newLandMercSearchResponse;
        }
        return null;
    }
}
